package com.luchao.back.service.impl;

import com.luchao.back.mapper.OrdersMapper;
import com.luchao.back.mapper.ReceivesMapper;
import com.luchao.back.pojo.Receives;
import com.luchao.back.service.ReceivesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ReceivesServiceImpl implements ReceivesService {

    @Autowired
    private ReceivesMapper receivesMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public List<Receives> getReceivesByUserId(Integer ruid) {
        return receivesMapper.getReceivesByUserId(ruid);
    }

    @Override
    public int addReceives(Receives receives) {
        return receivesMapper.addReceives(receives);
    }

    @Override
    public int editReceives(Receives receives) {
        return receivesMapper.editReceives(receives);
    }

    @Override
    public boolean delReceives(int recid) {
        boolean flag = true;
        if (ordersMapper.checkOrderUseReceives(recid) > 0) {
            flag = false;
        } else {
            receivesMapper.delReceives(recid);
        }
        return flag;
    }

    @Override
    @Transactional
    public boolean setDefaultReceives(Integer recid, Integer ruid) {
        boolean flag = true;
        try {
            if (receivesMapper.setMinorReceives(ruid) <= 0) throw new Exception();
            if (receivesMapper.setMainReceives(recid) <= 0) throw new Exception();
        } catch (Exception e) {
            flag = false;
        } finally {
            return flag;
        }
    }

}
